package com.concerneyes.glasses;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.concerneyes.R;
import com.concerneyes.glasses.adapter.GlassesAdapter;
import com.concerneyes.glasses.model.GlassesDetailModel;
import com.concerneyes.glasses.model.GlassesItemModel;
import com.concerneyes.glasses.task.GlassesImpl;
import com.concerneyes.glasses.task.GlassesListener;

import java.util.List;

import common.AbsBaseView;
import http.JsonMap;
/**
 * 产品View
 */
public class GlassesView extends AbsBaseView implements AdapterView.OnItemClickListener, GlassesListener{
    private ListView lvGlasses;
    private GlassesAdapter mAdapter;
    private Context mContext;
    private FrameLayout contentView;
    private GlassesImpl impl;
    private Activity activity;

    public GlassesView(Context context,Activity activity) {
        super(context);
        this.mContext = context;
        this.activity = activity;
        initView();
        loadData(null);
    }

    @Override
    public int layoutResId() {
        return R.layout.view_glasses;
    }

    public void initView() {
        lvGlasses = (ListView) findViewById(R.id.lv_glasses);
        contentView = (FrameLayout) findViewById(R.id.content_glasses_view);
        impl = new GlassesImpl(this);
        impl.categoryList();
    }

    private void initGlassesItemView(int categoryId) {
        GlassesInfoView infoView = new GlassesInfoView(mContext, categoryId,activity);
        contentView.removeAllViews();
        contentView.addView(infoView);
    }

    @Override
    public void loadData(Object obj) {
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mAdapter.changeSelected(position);//刷新
        JsonMap data = mAdapter.getItem(position);
        initGlassesItemView(data.getInt("id"));
    }

    @Override
    public void onCategoryListSuccess(List<JsonMap> datas) {
        mAdapter = new GlassesAdapter(mContext, datas);
        lvGlasses.setAdapter(mAdapter);
        lvGlasses.setOnItemClickListener(this);
        lvGlasses.setSelector(R.color.app_sky_blue);
        initGlassesItemView(datas.get(0).getInt("id"));
    }

    @Override
    public void onCategoryListFail() {

    }

    @Override
    public void onGlassesListSuccess(GlassesItemModel datas) {

    }

    @Override
    public void onGlassesListFail() {

    }

    @Override
    public void onGlassesInfoSuccess(GlassesDetailModel datas) {

    }

    @Override
    public void onGlassesInfoFail() {

    }

}
